//
//  WSQAccountTool.m
//  WSQOCTools
//
//  Created by 翁胜琼 on 2019/1/19.
//  Copyright © 2019 Dev John. All rights reserved.
//

#import "WSQAccountTool.h"
#import "WSQHttpRequest.h"
#import <MJExtension/MJExtension.h>
#import <SVProgressHUD/SVProgressHUD.h>

// MARK: - 内联函数
// 内联函数代替计算常量
static inline NSString* accountPath()
{
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *accountPath = [documentPath stringByAppendingPathComponent:@"account.data"];
    
    return accountPath;
}

@implementation WSQAccountTool
@synthesize account = _account;

+ (instancetype)shared
{
    static WSQAccountTool *accountTool = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        accountTool = [[self allocWithZone:NULL] init];
    });
    
    return accountTool;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _account = [NSKeyedUnarchiver unarchiveObjectWithFile:accountPath()];
    }
    return self;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    return [self shared];
}

- (void)saveAccount:(WSQAccount *)account
{
    // 只允许同时保存一个accout
    [self removeAccount];
    _account = account;
    [NSKeyedArchiver archiveRootObject:account toFile:accountPath()];
}

- (void)saveAccountWithDic:(NSDictionary *)dic
{
    WSQAccount *account = [WSQAccount mj_objectWithKeyValues:dic];
    [self saveAccount:account];
}

- (void)removeAccount
{
    if ([[NSFileManager defaultManager] fileExistsAtPath:accountPath()]) {
        [[NSFileManager defaultManager] removeItemAtPath:accountPath() error:nil];
    }
    _account = nil;
    
}

- (WSQAccount *)account
{
    _account = [NSKeyedUnarchiver unarchiveObjectWithFile:accountPath()];
    return _account;
}

// MARK: - 网络请求
// 登录
+ (void)loginWithUrl:(NSString *)url
              method:(WSQHttpRequestMethod)method
              params:(NSDictionary *)params
             Success:(void (^)(id response))success
             failure:(void (^)(NSError *error, NSInteger statusCode))failure
{    
    [self httprequestWithUrl:url method:method params:params Success:success failure:failure];
}

// 登出
+ (void)logoutWithUrl:(NSString *)url
              method:(WSQHttpRequestMethod)method
              params:(NSDictionary *)params
             Success:(void (^)(id response))success
             failure:(void (^)(NSError *error, NSInteger statusCode))failure
{
    [self httprequestWithUrl:url method:method params:params Success:success failure:failure];
}

// 重置密码
+ (void)resetPasswordWithUrl:(NSString *)url
               method:(WSQHttpRequestMethod)method
               params:(NSDictionary *)params
              Success:(void (^)(id response))success
              failure:(void (^)(NSError *error, NSInteger statusCode))failure
{
    [self httprequestWithUrl:url method:method params:params Success:success failure:failure];
}

// 注册
+ (void)registerWithUrl:(NSString *)url
                      method:(WSQHttpRequestMethod)method
                      params:(NSDictionary *)params
                     Success:(void (^)(id response))success
                     failure:(void (^)(NSError *error, NSInteger statusCode))failure
{
    [self httprequestWithUrl:url method:method params:params Success:success failure:failure];
}

// 绑定手机
+ (void)bindingPhoneWithUrl:(NSString *)url
                 method:(WSQHttpRequestMethod)method
                 params:(NSDictionary *)params
                Success:(void (^)(id response))success
                failure:(void (^)(NSError *error, NSInteger statusCode))failure
{
    [self httprequestWithUrl:url method:method params:params Success:success failure:failure];
}

// 绑定邮箱
+ (void)bindingEmailWithUrl:(NSString *)url
                     method:(WSQHttpRequestMethod)method
                     params:(NSDictionary *)params
                    Success:(void (^)(id response))success
                    failure:(void (^)(NSError *error, NSInteger statusCode))failure
{
    [self httprequestWithUrl:url method:method params:params Success:success failure:failure];
}

// 修改密码
+ (void)modifyPasswordWithUrl:(NSString *)url
                     method:(WSQHttpRequestMethod)method
                     params:(NSDictionary *)params
                    Success:(void (^)(id response))success
                    failure:(void (^)(NSError *error, NSInteger statusCode))failure
{
    [self httprequestWithUrl:url method:method params:params Success:success failure:failure];
}


// MARK: - Private Method
// 网络请求指定方法
+(void)httprequestWithUrl:(NSString *)url
                   method:(WSQHttpRequestMethod)method
                   params:(NSDictionary *)params
                  Success:(void (^)(id response))success
                  failure:(void (^)(NSError *error, NSInteger statusCode))failure
{
    switch (method) {
        case WSQHttpRequestMethodGET:
        {
            [self GET:url params:params Success:success failure:failure];
        }
            break;
            
        case WSQHttpRequestMethodPOST:
        {
            [self POST:url params:params Success:success failure:failure];
        }
            break;
    }
}

+(void)POST:(NSString *)url
     params:(NSDictionary *)params
    Success:(void (^)(id response))success
    failure:(void (^)(NSError *error, NSInteger statusCode))failure
{
    [WSQHttpRequest POST:url
              parameters:params
                   token:3
                 success:success
                 failure:failure];
}

+(void)GET:(NSString *)url
     params:(NSDictionary *)params
    Success:(void (^)(id response))success
    failure:(void (^)(NSError *error, NSInteger statusCode))failure
{
    [WSQHttpRequest GET:url
              parameters:params
                   token:3
                 success:success
                 failure:failure];
}
@end
